package com.hellojava.util;

import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.util.Util;
import com.hellojava.bean.OcrConstant;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author dengjihai
 * @create 2017-12-19
 **/
public class Sample {


    public static void main(String[] args) throws IOException {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(OcrConstant.APP_ID, OcrConstant.API_KEY, OcrConstant.SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

//        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 调用接口
        String path = "D:\\imgs\\w.jpg";
        byte[] imgData = Util.readFileByBytes(path);
        // 高精度识别
        JSONObject res = client.basicAccurateGeneral(imgData, new HashMap<String, String>());
        System.out.println("百度返回结果："+res.toString());
        JSONArray jsonArray=res.getJSONArray("words_result");
        JSONObject jsonObject= (JSONObject) jsonArray.get(0);
        System.out.println("##########  " +jsonObject.getString("words"));
        for (int i=0;i<jsonArray.length();i++){
            System.out.println(jsonArray.get(i));
        }
        System.out.println(res.toString(2));

    }

}
